package com.xuzz.study.thread.threadpool;

import com.xuzz.study.datastructure.LinkedBlockingQueue;

public class Test {
    public static void main(String[] args)
    {
        /**
             * 1、使用Executors创建线程池
             */
    //	ExecutorService executors = Executors.newFixedThreadPool(2);
    /**
         * 2、直接new ThreadPoolExecutor，并指定corePoolSize和maximumPoolSize不一样
         */
    ExecutorService executors = new ThreadPoolExecutor(1, 1,
                      new LinkedBlockingQueue<Runnable>(3), new MyThreadFactory());

    executors.execute(new Runnable() {
      @Override
      public void run() {
        int i = 0;
        while(i++ < 5) {
          System.out.println(Thread.currentThread().getName() + "-- 111");
          try {
            Thread.sleep(5000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    });
    executors.execute(new Runnable() {
      @Override
      public void run() {
        int i = 0;
        while(i++ < 5) {
          System.out.println(Thread.currentThread().getName() + "-- 222");
          try {
            Thread.sleep(5000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    });
    executors.execute(new Runnable() {
      @Override
      public void run() {
        int i = 0;
        while(i++ < 5) {
          System.out.println(Thread.currentThread().getName() + "-- 333");
          try {
            Thread.sleep(5000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    });
    executors.execute(new Runnable() {
      @Override
      public void run() {
        int i = 0;
        while(i++ < 5) {
          System.out.println(Thread.currentThread().getName() + "-- 444");
          try {
            Thread.sleep(5000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    });
    executors.execute(new Runnable() {
      @Override
      public void run() {
        int i = 0;
        while(i++ < 5) {
          System.out.println(Thread.currentThread().getName() + "-- 555");
          try {
            Thread.sleep(5000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    });
    executors.execute(new Runnable() {
      @Override
      public void run() {
        int i = 0;
        while(i++ < 5) {
          System.out.println(Thread.currentThread().getName() + "-- 666");
          try {
            Thread.sleep(5000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    });
    executors.execute(new Runnable() {
      @Override
      public void run() {
        int i = 0;
        while(i++ < 5) {
          System.out.println(Thread.currentThread().getName() + "-- 777");
          try {
            Thread.sleep(5000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    });
    executors.execute(new Runnable() {
      @Override
      public void run() {
        int i = 0;
        while(i++ < 5) {
          System.out.println(Thread.currentThread().getName() + "-- 888");
          try {
            Thread.sleep(5000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    });

    System.out.println("主函数结束");
    }
}
